//
//  ReplyView.m
//  cliqset
//
//  Created by David Hinson on 4/1/09.
//  Copyright 2009 Cliqset, Inc.. All rights reserved.
//

#import "ReplyView.h"
#import "MessageDetailViewController.h"
#import "cliqsetAppDelegate.h"
#import "Message.h"

@implementation ReplyView

@synthesize replyField;
@synthesize parent;

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        // Initialization code
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    // Drawing code
}

// Do Reply
-(IBAction)doReply:(id)id {	
	cliqsetAppDelegate* myApp = (cliqsetAppDelegate*)[[UIApplication sharedApplication] delegate];
	
	MessageDetailViewController * myParent = (MessageDetailViewController*)self.parent;
	[self.replyField resignFirstResponder];
	
	[myParent showReplyView:NO];	
	
	NSMutableArray * recipients = [[NSMutableArray alloc] init];
	
	Message * message = (Message *)[myApp.messages objectAtIndex:myParent.index];
	
	[recipients addObject:message.sender.userIdentifier];
	
	NSMutableString * subject;
	NSRange reStartRange = [message.subject rangeOfString:@"Re: " options: NSCaseInsensitiveSearch];
	if (reStartRange.location == NSNotFound) {
		subject = (NSMutableString*)[NSString stringWithFormat:@"Re: %@", message.subject];
	} else {
		subject = (NSMutableString*)[NSString stringWithFormat:@"%@", message.subject];
	}
	
	[myApp.cliqset sendMessage:subject message:self.replyField.text recipients:recipients];
	[recipients release];
}

// Do Cancel

-(IBAction)doCancel:(id)id {
	MessageDetailViewController * myParent = (MessageDetailViewController*)self.parent;
	[self.replyField resignFirstResponder];
	[myParent showReplyView:NO];
}

- (void)dealloc {
	[parent release];
	[replyField release];
    [super dealloc];
}

@end
